/**
 * www.lifexx.com Inc.
 * Copyright (c) 2014 All Rights Reserved.
 */
package net.mikoo.seals.agent.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import net.mikoo.seals.agent.biz.services.MenuService;
import net.mikoo.seals.agent.biz.services.RolePermitService;
import net.mikoo.seals.agent.common.BaseController;
import net.mikoo.seals.agent.model.Menu;

/**
 *                       
 * @Filename RolePermitController.java
 *
 * @Description 
 *
 * @Version 1.0
 *
 * @Author chenzx
 *
 * @Email chenzx1206@163.com
 *       
 * @History
 *<li>Author: chenzx</li>
 *<li>Date: 2015年1月27日</li>
 *<li>Version: 1.0</li>
 *<li>Content: create</li>
 *
 */
@Controller
@RequestMapping("/admin/rolePermit")
public class RolePermitController extends BaseController {
	
	@Autowired
	private RolePermitService	rolePermitService;
	
	@Autowired
	private MenuService			menuService;
	
	@RequestMapping("/form")
	public String from(Long roleId,Model model) {
		
		try {
			
			List<Menu> menus = menuService.loadAsTree();
			
			List<Menu> haveMenus = rolePermitService.loadResourceByRoleId(roleId);
			model.addAttribute("menus", menus);
			model.addAttribute("haveMenus", haveMenus);
			model.addAttribute("roleId", roleId);
			return resultView(ADMIN_VIEW, "role/permit/form");
			
		}catch (Exception e) {
			return this.errorTips("", e.getMessage(), model);
		}
		
	}
	
	@RequestMapping("/create")
	public String create(HttpServletRequest request, Model model, Long roleId, String[] menuIds) {

		try {
			rolePermitService.updateByRoleIdAndMenuId(roleId, menuIds);
			return dialogResult(model, "add");
		} catch (Exception e) {
			return this.errorTips("", e.getMessage(), model);
		}

	}
	
	
}
